#!/bin/sh

## chang here
SERVICE_DIR=/opt/wesc
SERVICE_NAME=boot-core-webapi
SPRING_PROFILES_ACTIVE=prod

case "$1" in
	start)
		procedure=`ps -ef | grep -w "${SERVICE_DIR}/${SERVICE_NAME}" |grep -w "java"| grep -v "grep" | awk '{print $2}'`
		if [ "${procedure}" = "" ];
		then
			echo "start ..."
			if [ "$2" != "" ];
			then
				SPRING_PROFILES_ACTIVE=$2
			fi
			echo "spring.profiles.active=${SPRING_PROFILES_ACTIVE}"
			source /etc/profile
      exec nohup java -Xms1024m -Xmx1024m -Xmn512m -Dfile.encoding=utf-8 -jar ${SERVICE_DIR}/${SERVICE_NAME}-1.0.0\.jar --spring.profiles.active=${SPRING_PROFILES_ACTIVE} 2>&1 &
			echo "start success"
		else
			echo "${SERVICE_NAME} is start"
		fi
		;;

	stop)
		procedure=`ps -ef | grep -w "${SERVICE_DIR}/${SERVICE_NAME}" |grep -w "java"| grep -v "grep" | awk '{print $2}'`
		if [ "${procedure}" = "" ];
		then
			echo "${SERVICE_DIR}/${SERVICE_NAME} is stop"
		else
			kill -9 ${procedure}
			sleep 1
			argprocedure=`ps -ef | grep -w "${SERVICE_DIR}/${SERVICE_NAME}" |grep -w "java"| grep -v "grep" | awk '{print $2}'`
			if [ "${argprocedure}" = "" ];
			then
				echo "${SERVICE_DIR}/${SERVICE_NAME} stop success"
			else
				kill -9 ${argprocedure}
				echo "${SERVICE_DIR}/${SERVICE_NAME} stop error"
			fi
		fi
		;;

	restart)
		$0 stop
		sleep 1
		$0 start $2
		;;

	*)
		echo "usage: $0 [start|stop|restart] [dev|sand|prod|prod-backup]"
		;;
esac